<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="1000" top="100px" destroy-on-close
		@closed="$emit('closed')">
		<el-form :model="FormData" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
			<el-form-item label="门店名称" prop="name">
				{{FormData.name}}
			</el-form-item>
			<el-form-item label="营业时间" prop="businessStart">
				{{FormData.businessStart}} - {{FormData.businessEnd}}
			</el-form-item>
			<el-form-item label="自动撤布防" prop="businessStart">
				<el-switch v-model="submitForm.autoDeploying"></el-switch>
			</el-form-item>
			<el-form-item v-if="submitForm.autoDeploying || submitForm.autoDeploying==='true'" label="" prop="businessStart">
				<el-checkbox v-model="submitForm.startEnable"></el-checkbox>
				根据开始营业时间顺延<el-input v-model="submitForm.startDelay" placeholder="请输入数字" style="width:100px;" />分钟自动
				<el-select v-model="submitForm.startOption" style="width:100px">
					<el-option value="撤防" label="撤防"></el-option>
					<el-option value="布防" label="布防"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item v-if="submitForm.autoDeploying || submitForm.autoDeploying==='true'" label="" prop="businessStart">
				<el-checkbox v-model="submitForm.endEnable"></el-checkbox>
				根据营业结束时间提前<el-input v-model="submitForm.endDelay" placeholder="请输入数字" style="width:100px;" />分钟自动
				<el-select v-model="submitForm.endOption" style="width:100px">
					<el-option value="布防" label="布防"></el-option>
					<el-option value="撤防" label="撤防"></el-option>
				</el-select>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">返 回</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				showAMap: false,
				mode: "add",
				titleMap: {
					add: '门店配置',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				FormData: {
					name: null,
					unit: {}
				},
				submitForm: {
					autoDeploying: false,
					startEnable:true,
					startDelay:'0',
					startOption:"撤防",
					endEnable:false,
					endDelay:'0',
					endOption:"撤防"
				},
				//验证规则
				rules: {
					name: [{
						required: true,
						message: '请输入姓名'
					}]
				},
				areaProps: {
					value: 'id',
					label: 'name',
					checkStrictly: false
				},
				option: [{
						"name": "autoDeploying",
						"value": "false"
					}, //对应自动撤布防 开关项
					{
						"name": "startEnable",
						"value": "false"
					},
					//对应这行的勾选框
					{
						"name": "startDelay",
						"value": "0"
					},
					//对应 开始营业时间顺延 后面的数字
					{
						"name": "startOption",
						"value": "撤防"
					},
					//对应这行的 下拉框
					{
						"name": "endEnable",
						"value": "false"
					},
					//对应这行的勾选框
					{
						"name": "endDelay",
						"value": "0"
					},
					//对应 结束营业时间顺延 后面的数字
					{
						"name": "endOption",
						"value": "撤防"
					}
					//对应这行的 下拉框
				]
			}
		},
		watch: {
			'submitForm.autoDeploying'(nVal) {
				if(nVal === true){
					this.option[0].value = 'true'
				}else{
					this.option[0].value = 'false'
				}
			},
			'submitForm.startEnable'(nVal) {
				if(nVal === true){
					this.option[1].value = 'true'
				}else{
					this.option[1].value = 'false'
				}
			},
			'submitForm.startDelay'(nVal) {
				this.option[2].value = nVal
			},
			'submitForm.startOption'(nVal) {
				this.option[3].value = nVal
			},
			'submitForm.endEnable'(nVal) {
				if(nVal === true){
					this.option[4].value = 'true'
				}else{
					this.option[4].value = 'false'
				}
			},
			'submitForm.endDelay'(nVal) {
				this.option[5].value = nVal
			},
			'submitForm.endOption'(nVal) {
				this.option[6].value = nVal
			}
		},
		mounted() {},
		methods: {
			//显示
			open(mode = 'add') {
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			async submit() {
				var valid = await this.$refs.dialogForm.validate().catch(() => {})
				if (!valid) {
					return false
				}
				this.isSaveing = true;

				if (this.FormData.region && this.FormData.region.length) {
					this.FormData.region = this.FormData.region[this.FormData.region.length - 1]
				}
				this.FormData.unit = {
					id: this.FormData.unit.id
				}
				var res = await this.$API.enterprise.store.config.post(this.option);
				this.isSaveing = false;
				if (res.code == 200) {
					// if (this.mode == 'add') {
					// 	this.FormData.id = res.data
					// }
					this.$emit('success', this.FormData, this.mode)
					this.visible = false;
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//表单注入数据
			setData(data) {
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.FormData, data)
				//delete this.FormData.properties
				// delete this.FormData.createTime
				// delete this.FormData.lastModifyTime
				// delete this.FormData.auditingTime
				// delete this.FormData.updateByName
				// delete this.FormData.createByName
				// delete this.FormData.auditing
				// delete this.FormData.version
				// delete this.FormData.auditingByName
				// delete this.FormData.createBy
				// delete this.FormData.deleted
				// delete this.FormData.auditingBy
				// delete this.FormData.updateBy
				// this.FormData.unit.id = data.unitId
				// this.FormData.unit.name = data.unitName
				this.FormData.id = data.id
				this.FormData.name = data.name
				this.FormData.businessStart = data.businessStart
				this.FormData.businessEnd = data.businessEnd
				this.option[0].id = this.FormData.id
				this.option[1].id = this.FormData.id
				this.option[2].id = this.FormData.id
				this.option[3].id = this.FormData.id
				this.option[4].id = this.FormData.id
				this.option[5].id = this.FormData.id
				this.option[6].id = this.FormData.id

				if(data.properties && data.properties.autoDeploying){
					//this.submitForm = data.properties
					this.submitForm.startOption = data.properties.startOption
					this.submitForm.endOption = data.properties.endOption
					if(data.properties.autoDeploying === 'true' || data.properties.autoDeploying === true){
						this.submitForm.autoDeploying = true
					}else{
						this.submitForm.autoDeploying = false
					}
					if(data.properties.startEnable === 'true' || data.properties.startEnable === true){
						this.submitForm.startEnable = true
					}else{
						this.submitForm.startEnable = false
					}
					if(data.properties.endEnable === 'true' || data.properties.endEnable === true){
						this.submitForm.endEnable = true
					}else{
						this.submitForm.endEnable = false
					}
					this.submitForm.startDelay = data.properties.startDelay || '0'
					this.submitForm.endDelay = data.properties.endDelay || '0'
				}


			}
		},
		beforeUnmount(){
			this.submitForm = {}
		}
	}
</script>

<style lang="scss" scoped>
	.el-input {
		border-width: 0 0 1px 0 !important;
	}
</style>
